package com.whu.exp1jsp.dao;


import com.whu.exp1jsp.pojo.CartSt;
import com.whu.exp1jsp.pojo.ProductSt;
import com.whu.exp1jsp.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import java.util.ArrayList;
import java.util.List;

public class CartDAO {

    // 数据库连接信息
    // 查询购物车
    public List<CartSt> selectCartByUserId(long userId) throws SQLException {
        List<CartSt> cartList = new ArrayList<>();
        String sql = "SELECT user_id AS userId, product_id AS productId, updated_time AS updatedTime FROM cart_st WHERE user_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    CartSt cart = new CartSt();
                    cart.setUserId(rs.getLong("userId"));
                    cart.setProductId(rs.getLong("productId"));
                    cart.setUpdatedTime(rs.getTimestamp("updatedTime"));
                    cartList.add(cart);
                }
            }
        }
        return cartList;
    }

    // 添加购物车
    public int insertCart(CartSt cart) throws SQLException {
        String sql = "INSERT INTO cart_st (user_id, product_id, updated_time) VALUES (?, ?, CURRENT_TIMESTAMP)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, cart.getUserId());
            ps.setLong(2, cart.getProductId());
            return ps.executeUpdate();
        }
    }

    // 删除购物车
    public int deleteCart(long userId, long productId) throws SQLException {
        String sql = "DELETE FROM cart_st WHERE user_id = ? AND product_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, userId);
            ps.setLong(2, productId);
            return ps.executeUpdate();
        }
    }

    // 购买商品（从购物车中删除该商品，并增加购买记录）
    public int purchaseProduct(long userId, long productId) throws SQLException {
        String sql = "DELETE FROM cart_st WHERE user_id = ? AND product_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, userId);
            ps.setLong(2, productId);
            return ps.executeUpdate();
        }
    }

    // 获取购物车中的商品列表
    public List<ProductSt> getCartProducts(Long userId) throws SQLException {
        List<ProductSt> productList = new ArrayList<>();
        String sql = "SELECT p.product_id AS productId, p.product_title AS productTitle, p.product_intro AS productIntro, " +
                "p.product_price AS productPrice, p.product_imgs AS productImgs, p.publish_time AS publishTime, " +
                "p.owner_id AS ownerId, p.location AS location, p.status AS status " +
                "FROM product_st p " +
                "JOIN cart_st c ON p.product_id = c.product_id " +
                "WHERE c.user_id = ? " +
                "ORDER BY c.updated_time DESC";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setLong(1, userId);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    ProductSt product = new ProductSt();
                    product.setProductId(rs.getLong("productId"));
                    product.setProductTitle(rs.getString("productTitle"));
                    product.setProductIntro(rs.getString("productIntro"));
                    product.setProductPrice(rs.getDouble("productPrice"));
                    product.setProductImgs(rs.getString("productImgs"));
                    product.setPublishTime(rs.getTimestamp("publishTime").toLocalDateTime());
                    product.setOwnerId(rs.getLong("ownerId"));
                    product.setLocation(rs.getString("location"));
                    product.setStatus(rs.getInt("status"));
                    productList.add(product);
                }
            }
        }
        return productList;
    }
}
